package com.itheima.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.project.constant.ShopConstant;
import com.itheima.project.mapper.BrandMapper;
import com.itheima.project.pojo.Brand;
import com.itheima.project.service.BrandService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.vo.BrandVo;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @ClassName 类名
 * @Description 类说明
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {
    @Override
    public Page<Brand> findBrandVoPage(BrandVo brandVo, int pageNum, int pageSize) {
        // 构建分页信息
        Page<Brand> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<Brand> queryWrapper = Wrappers.lambdaQuery();
        // 品牌查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getBrandName())) {
            queryWrapper.like(Brand::getBrandName,brandVo.getBrandName());
        }
        // 分类查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getCategory())) {
            queryWrapper.eq(Brand::getCategory,brandVo.getCategory());
        }
        // 状态查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getEnableFlag())) {
            queryWrapper.eq(Brand::getEnableFlag,brandVo.getEnableFlag());
        }
        // 创建时间降序
        queryWrapper.orderByDesc(Brand::getCreatedTime);
        // 执行分页查询返回分页结果
        return page(page, queryWrapper);
    }
    @Override
    public Brand createBrand(BrandVo brandVo) {
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        return save(brand) ? brand : null;
    }

    @Override
    public Boolean updateBrand(BrandVo brandVo) {
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        return updateById(brand);
    }

    @Override
    public Boolean deleteBrand(List<Long> checkedIds) {
        return removeByIds(checkedIds);
    }

    @Override
    public List<Brand> findBrandVoList() {
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery();
        wrapper.eq(Brand::getEnableFlag, ShopConstant.YES);
        return list(wrapper);
    }
}
